//
//  XYHomeViewController.swift
//  SinaWeibo
//
//  Created by 韩啸宇 on 15/12/19.
//  Copyright © 2015年 backy. All rights reserved.
//

import UIKit

class XYHomeViewController: XYBaseTableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.设置访客视图
        guard isLogin else {
            visitorView?.setVisitorInfo(nil, title: "关注一些人, 回这里看见有什么惊喜")
            return
        }
        
        // 2.初始化导航条
        setUpNav()
    }
    
    // Mark: - 初始化
    private func setUpNav() {
        // 1.添加首页导航条按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, action: Selector("leftBtnClick"), imageName: "navigationbar_friendattention")
        navigationItem.rightBarButtonItem = UIBarButtonItem(target: self, action: Selector("rightBtnClick"), imageName: "navigationbar_pop")
        
        // 2.添加导航栏标题按钮
        let titleView = XYTitleButton(title: "哈哈哈", imageName: "navigationbar_arrow_down", selectedImageName: "navigationbar_arrow_up", target: self, action: Selector("titleBtnClick:"))

        navigationItem.titleView = titleView
    }
    
    // MARK: - 事件处理
    @objc private func leftBtnClick() {
        XYLog()
    }
    
    @objc private func rightBtnClick() {
        XYLog()
    }
    
    @objc private func titleBtnClick(sender: UIButton) {
        // 要想既显示弹出的视图, 又显示以前的视图, 就要自定义转场动画
        // 如果要想修改修改弹出视图的尺寸, 也要自定义转场动画
        
        sender.selected = !sender.selected

        // 1.创建控制器
        let vc = UIStoryboard(name: "Popover", bundle: nil).instantiateInitialViewController()!
        
        // 2.自定义转场
        // 2.1 设置自定义转场代理
        vc.transitioningDelegate = presentationManager
        // 2.2 设置自定义转场样式
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        // 2.3 设置转场尺寸
        presentationManager.presentSize = CGSizeMake(200, 400)
        
        // 2.弹出控制器
        presentViewController(vc, animated: true, completion: nil)
        
    }
    
    // MARK: - 懒加载
    /// 转场管理者
    private lazy var presentationManager = XYPresentationManager()
}